package com.tianpengcc.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tianpengcc.model.DateType;
import com.tianpengcc.model.DirayType;
import com.tianpengcc.model.custom.DirayTypeCustom;
import com.tianpengcc.service.DirayService;
import com.tianpengcc.service.DirayTypeService;
import com.tianpengcc.service.impl.DirayServiceImpl;
import com.tianpengcc.service.impl.DirayTypeServiceImpl;
import com.tianpengcc.util.PropertiesUtil;

public class DirayTypeServlet extends HttpServlet {

	private DirayService dirayService = new DirayServiceImpl() ;

	private DirayTypeService dirayTypeService = new DirayTypeServiceImpl() ;

	/**
	 * <p>Description:</p>
	 * <p>Compary</p>
	 * @author 田鹏
	 * @date 2017年10月27日下午4:17:16
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String method = request.getParameter("method");
			String resultPath = "" ;
			if("list".equals(method)){
				resultPath = list(request,response) ;
			}else if("add".equals(method)){
				resultPath = add(request,response) ;
			}else if("edit".equals(method)){
				resultPath = edit(request,response) ;
			}else if("delete".equals(method)){
				try{
					resultPath = delete(request,response) ;
				}catch(Exception e){
					request.setAttribute("error", "该日记类别下有日记不能删除");
					request.getRequestDispatcher("/error.jsp").forward(request, response);
					return ;
				}
			}else if("addUI".equals(method)){
				resultPath = addUI(request,response) ;
			}else if("editUI".equals(method)){
				resultPath = editUI(request,response) ;
			}
			
			List<DirayTypeCustom> dirayTypeList = dirayTypeService.getAll();
			List<DateType> dateTypeList = dirayService.getDateTypeBySize(Integer.parseInt(PropertiesUtil.getValue("dateTypeSize")));
			
			String[] actionMethod = resultPath.split(":");
			if("dispatcher".equals(actionMethod[0])){
				request.setAttribute("mainPage", actionMethod[1]);
				request.setAttribute("dateTypeList", dateTypeList);
				request.setAttribute("dirayTypeList", dirayTypeList);
				request.getRequestDispatcher("/main.jsp").forward(request, response) ;
			}else{
				response.sendRedirect(request.getContextPath() + actionMethod[1]);
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("error", "多半是错误了！");
			request.getRequestDispatcher("/error.jsp").forward(request, response);
		}
	}

	public String addUI(HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		return "dispatcher:/dirayType/dirayTypeAdd.jsp";
	}

	public String editUI(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		String dirayTypeId = request.getParameter("dirayTypeId");
		DirayType dirayType = dirayTypeService.getById(Integer.parseInt(dirayTypeId));
		request.setAttribute("dirayType", dirayType);
		return "dispatcher:/dirayType/dirayTypeEdit.jsp";
	}

	public String delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		String dirayTypeId = request.getParameter("dirayTypeId");
		dirayTypeService.doDelete(Integer.parseInt(dirayTypeId));
		return "sendRedirect:/dirayType?method=list";
	}

	public String edit(HttpServletRequest request, HttpServletResponse response)
		throws Exception{
		String dirayTypeId = request.getParameter("dirayTypeId");
		String dirayTypeName = request.getParameter("dirayTypeName");
		DirayType dirayType = new DirayType() ;
		dirayType.setDirayTypeId(Integer.parseInt(dirayTypeId));
		dirayType.setDirayTypeName(dirayTypeName);
		dirayTypeService.doUpdate(dirayType.getDirayTypeId(),dirayType) ;
		return "sendRedirect:/dirayType?method=list";
	}

	public String add(HttpServletRequest request, HttpServletResponse response) 
		throws Exception{
		String dirayTypeName = request.getParameter("dirayTypeName");
		DirayType dirayType = new DirayType() ;
		dirayType.setDirayTypeName(dirayTypeName);
		dirayTypeService.doAdd(dirayType);
		return "sendRedirect:/dirayType?method=list";
	}

	public String list(HttpServletRequest request, HttpServletResponse response) {
		
		return "dispatcher:/dirayType/dirayTypeList.jsp";
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response) ;
	}

}
